/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package aptech.semester.code.gui;

import aptech.semester.code.common.ApplicationCommon;
import aptech.semester.code.data.BankList;
import aptech.semester.code.data.DepartmentList;
import aptech.semester.code.data.EducationList;
import aptech.semester.code.data.EmployeeList;
import aptech.semester.code.data.EmployeeSalaryList;
import aptech.semester.code.data.PositionList;
import aptech.semester.code.data.ProvinceList;
import aptech.semester.code.data.SalaryFormList;
import aptech.semester.code.data.SpecializedList;
import aptech.semester.code.entity.BankEntity;
import aptech.semester.code.entity.DepartmentEntity;
import aptech.semester.code.entity.EducationEntity;
import aptech.semester.code.entity.EmployeeEntity;
import aptech.semester.code.entity.EmployeeSalaryEntity;
import aptech.semester.code.entity.PositionEntity;
import aptech.semester.code.entity.ProvinceEntity;
import aptech.semester.code.entity.ReportSalaryEntity;
import aptech.semester.code.entity.SalaryFormEntity;
import aptech.semester.code.entity.SpecializedEntity;
import aptech.semester.code.item.BankComboItem;
import aptech.semester.code.item.DepartmentComboItem;
import aptech.semester.code.item.EducationComboItem;
import aptech.semester.code.item.PositionComboItem;
import aptech.semester.code.item.ProvinceComboItem;
import aptech.semester.code.item.SpecializedComboItem;
import com.toedter.calendar.JMonthChooser;
import java.awt.Dimension;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.TreeMap;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 *
 * @author vi
 */
public class EmployeeReport extends javax.swing.JInternalFrame {

    
    public EmployeeReport(MainScreen main) {
        initComponents();
//        initModelReport();
        cbDetailFilter.setEnabled(false);
        setSize(new Dimension(main.getWidth() - 5, main.getHeight() - 91));
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnReport = new javax.swing.JTabbedPane();
        pnPayroll = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        chooserYear = new com.toedter.calendar.JYearChooser();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblReportSalary = new javax.swing.JTable();
        pnEmployeeInformation = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        btnReportEmployee = new javax.swing.JButton();
        btnExportEmployee = new javax.swing.JButton();
        cbFilter = new javax.swing.JComboBox();
        cbDetailFilter = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jPanel7 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbReportEmployee = new javax.swing.JTable();

        setClosable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Employee Report");
        setPreferredSize(new java.awt.Dimension(1000, 600));

        pnReport.setAlignmentX(0.0F);
        pnReport.setAlignmentY(0.0F);
        pnReport.setMinimumSize(new java.awt.Dimension(980, 580));
        pnReport.setPreferredSize(new java.awt.Dimension(980, 580));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Criteria"));

        jLabel8.setText("Year");

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/report.png"))); // NOI18N
        jButton3.setText("Report");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/excel.png"))); // NOI18N
        jButton4.setText("Export To Excel");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(429, 429, 429)
                .addComponent(jLabel8)
                .addGap(18, 18, 18)
                .addComponent(chooserYear, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton3)
                .addGap(18, 18, 18)
                .addComponent(jButton4)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton3)
                        .addComponent(jButton4))
                    .addComponent(chooserYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Result"));
        jPanel4.setPreferredSize(new java.awt.Dimension(464, 481));

        tblReportSalary.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Employee ID", "Employee Name", "Department", "Position", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", "Total"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Object.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblReportSalary.setRowHeight(30);
        tblReportSalary.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(tblReportSalary);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1333, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 515, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout pnPayrollLayout = new javax.swing.GroupLayout(pnPayroll);
        pnPayroll.setLayout(pnPayrollLayout);
        pnPayrollLayout.setHorizontalGroup(
            pnPayrollLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, 1345, Short.MAX_VALUE)
        );
        pnPayrollLayout.setVerticalGroup(
            pnPayrollLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnPayrollLayout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, 538, Short.MAX_VALUE))
        );

        pnReport.addTab("Payroll", pnPayroll);

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder("Criteria"));

        jLabel9.setText("Report Employee by: ");

        btnReportEmployee.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/report.png"))); // NOI18N
        btnReportEmployee.setText("Report");
        btnReportEmployee.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReportEmployeeActionPerformed(evt);
            }
        });

        btnExportEmployee.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/excel.png"))); // NOI18N
        btnExportEmployee.setText("Export To Excel");
        btnExportEmployee.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExportEmployeeActionPerformed(evt);
            }
        });

        cbFilter.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-----Select-----", "Department", "Position", "Province", "Specialize", "Education level" }));
        cbFilter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbFilterActionPerformed(evt);
            }
        });

        jLabel4.setText("Details");

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addComponent(cbFilter, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(48, 48, 48)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(cbDetailFilter, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnReportEmployee)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnExportEmployee)
                .addContainerGap())
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel9)
                        .addComponent(cbFilter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(cbDetailFilter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel4))
                    .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnReportEmployee)
                        .addComponent(btnExportEmployee)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jScrollPane2.setBorder(javax.swing.BorderFactory.createTitledBorder("Table Result"));

        tbReportEmployee.setAutoCreateRowSorter(true);
        tbReportEmployee.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Employee ID", "Name", "Gender", "Marital Status", "Phone", "Entry Date", "Basic Salary", "Province", "Department", "Position", "Education level", "Specialize"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Object.class, java.lang.Object.class, java.lang.String.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.String.class, java.lang.String.class, java.lang.Object.class, java.lang.Object.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane2.setViewportView(tbReportEmployee);
        if (tbReportEmployee.getColumnModel().getColumnCount() > 0) {
            tbReportEmployee.getColumnModel().getColumn(0).setMinWidth(90);
            tbReportEmployee.getColumnModel().getColumn(0).setPreferredWidth(90);
            tbReportEmployee.getColumnModel().getColumn(0).setMaxWidth(90);
            tbReportEmployee.getColumnModel().getColumn(1).setMinWidth(170);
            tbReportEmployee.getColumnModel().getColumn(1).setPreferredWidth(170);
            tbReportEmployee.getColumnModel().getColumn(1).setMaxWidth(170);
            tbReportEmployee.getColumnModel().getColumn(2).setMinWidth(100);
            tbReportEmployee.getColumnModel().getColumn(2).setPreferredWidth(100);
            tbReportEmployee.getColumnModel().getColumn(2).setMaxWidth(100);
            tbReportEmployee.getColumnModel().getColumn(3).setMinWidth(100);
            tbReportEmployee.getColumnModel().getColumn(3).setPreferredWidth(100);
            tbReportEmployee.getColumnModel().getColumn(3).setMaxWidth(100);
            tbReportEmployee.getColumnModel().getColumn(4).setMinWidth(100);
            tbReportEmployee.getColumnModel().getColumn(4).setPreferredWidth(100);
            tbReportEmployee.getColumnModel().getColumn(4).setMaxWidth(100);
            tbReportEmployee.getColumnModel().getColumn(5).setMinWidth(100);
            tbReportEmployee.getColumnModel().getColumn(5).setPreferredWidth(100);
            tbReportEmployee.getColumnModel().getColumn(5).setMaxWidth(100);
            tbReportEmployee.getColumnModel().getColumn(6).setMinWidth(120);
            tbReportEmployee.getColumnModel().getColumn(6).setPreferredWidth(120);
            tbReportEmployee.getColumnModel().getColumn(6).setMaxWidth(120);
            tbReportEmployee.getColumnModel().getColumn(7).setMinWidth(100);
            tbReportEmployee.getColumnModel().getColumn(7).setPreferredWidth(100);
            tbReportEmployee.getColumnModel().getColumn(7).setMaxWidth(100);
            tbReportEmployee.getColumnModel().getColumn(8).setMinWidth(120);
            tbReportEmployee.getColumnModel().getColumn(8).setPreferredWidth(120);
            tbReportEmployee.getColumnModel().getColumn(8).setMaxWidth(120);
            tbReportEmployee.getColumnModel().getColumn(9).setMinWidth(120);
            tbReportEmployee.getColumnModel().getColumn(9).setPreferredWidth(120);
            tbReportEmployee.getColumnModel().getColumn(9).setMaxWidth(120);
            tbReportEmployee.getColumnModel().getColumn(10).setMinWidth(120);
            tbReportEmployee.getColumnModel().getColumn(10).setPreferredWidth(120);
            tbReportEmployee.getColumnModel().getColumn(10).setMaxWidth(120);
        }

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 1360, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 527, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout pnEmployeeInformationLayout = new javax.swing.GroupLayout(pnEmployeeInformation);
        pnEmployeeInformation.setLayout(pnEmployeeInformationLayout);
        pnEmployeeInformationLayout.setHorizontalGroup(
            pnEmployeeInformationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnEmployeeInformationLayout.createSequentialGroup()
                .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        pnEmployeeInformationLayout.setVerticalGroup(
            pnEmployeeInformationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnEmployeeInformationLayout.createSequentialGroup()
                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pnReport.addTab("Employee Information", pnEmployeeInformation);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(pnReport, javax.swing.GroupLayout.PREFERRED_SIZE, 1350, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(pnReport, javax.swing.GroupLayout.PREFERRED_SIZE, 650, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        reportSalary();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        JFileChooser fc = new JFileChooser();
        int option = fc.showSaveDialog(this);
        
        if (option == JFileChooser.APPROVE_OPTION) {
            String filename = fc.getSelectedFile().getName();
            String path = fc.getSelectedFile().getParentFile().getPath();

            int len = filename.length();
            String ext = "";
            String file = "";

            if (len > 4) {
                ext = filename.substring(len - 4, len);
            }

            if (ext.equals(".xls")) {
                file = path + "\\" + filename;
            } else {
                file = path + "\\" + filename + ".xls";
            }
            
            new ApplicationCommon().writeToExcell(tblReportSalary, file,"REPORT SALARY IN "+chooserYear.getYear());
        }
        
    }//GEN-LAST:event_jButton4ActionPerformed

    private void btnReportEmployeeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnReportEmployeeActionPerformed
        if (cbFilter.getSelectedIndex() == 0) {
            JOptionPane.showMessageDialog(null, "Please choose filter to report!");
            cbFilter.requestFocus();
            return;
        } else {
             model = (DefaultTableModel)tbReportEmployee.getModel();
            if (cbFilter.getSelectedItem().toString().equals("Department")) {
               DepartmentComboItem dept = (DepartmentComboItem) cbDetailFilter.getSelectedItem();
                int deptID = dept.getValue().getDepartmentId();
                List<EmployeeEntity> listEmp = new EmployeeList().getEmployeeByDepartment(deptID);
                model.setRowCount(0);
                if (listEmp.size() != 0) {
                    for (EmployeeEntity item : listEmp) {
                        model.addRow(item.toVector());
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "Not found any data of employee !");
                }
            }
            if (cbFilter.getSelectedItem().toString().equals("Position")) {
               PositionComboItem dept = (PositionComboItem) cbDetailFilter.getSelectedItem();
                int posiID = dept.getValue().getPositionId();
                List<EmployeeEntity> listEmp = new EmployeeList().getEmployeeByPosition(posiID);
                  model.setRowCount(0);
                if (listEmp.size() != 0) {
                    for (EmployeeEntity item : listEmp) {
                        model.addRow(item.toVector());
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "Not found any data of employee !");
                }
            }
            if (cbFilter.getSelectedItem().toString().equals("Province")) {
               ProvinceComboItem dept = (ProvinceComboItem) cbDetailFilter.getSelectedItem();
                int provID = dept.getValue().getProvinceId();
                List<EmployeeEntity> listEmp = new EmployeeList().getEmployeeByProvince(provID);
                model.setRowCount(0);
                if (listEmp.size() != 0) {
                    for (EmployeeEntity item : listEmp) {
                        model.addRow(item.toVector());
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "Not found any data of employee !");
                }
            }
            if (cbFilter.getSelectedItem().toString().equals("Specialize")) {
               SpecializedComboItem dept = (SpecializedComboItem) cbDetailFilter.getSelectedItem();
                int specID = dept.getValue().getSpecializedId();
                List<EmployeeEntity> listEmp = new EmployeeList().getEmployeeBySpecialize(specID);
                model.setRowCount(0);
                if (listEmp.size() != 0) {
                    for (EmployeeEntity item : listEmp) {
                        model.addRow(item.toVector());
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "Not found any data of employee !");
                }
            }
            if (cbFilter.getSelectedItem().toString().equals("Education level")) {
               EducationComboItem dept = (EducationComboItem) cbDetailFilter.getSelectedItem();
                int eduID = dept.getValue().getEducationId();
                List<EmployeeEntity> listEmp = new EmployeeList().getEmployeeByEducation(eduID);
                model.setRowCount(0);
                if (listEmp.size() != 0) {
                    for (EmployeeEntity item : listEmp) {
                        model.addRow(item.toVector());
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "Not found any data of employee !");
                }
            }
            
            
            
        }
        
    }//GEN-LAST:event_btnReportEmployeeActionPerformed

    private void btnExportEmployeeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExportEmployeeActionPerformed
        JFileChooser fc = new JFileChooser();
        int option = fc.showSaveDialog(this);
        
        if (option == JFileChooser.APPROVE_OPTION) {
            String filename = fc.getSelectedFile().getName();
            String path = fc.getSelectedFile().getParentFile().getPath();

            int len = filename.length();
            String ext = "";
            String file = "";

            if (len > 4) {
                ext = filename.substring(len - 4, len);
            }

            if (ext.equals(".xls")) {
                file = path + "\\" + filename;
            } else {
                file = path + "\\" + filename + ".xls";
            }
            String title = "REPORT ";
            if (cbFilter.getSelectedItem().equals("Department")) {
                title += "DEPARTMENT ";
                DepartmentComboItem item = (DepartmentComboItem)cbDetailFilter.getSelectedItem();
                title += item.getValue().getDepartmentName().toUpperCase();
            } else if (cbFilter.getSelectedItem().equals("Position")) {
                title += "POSITION ";
                PositionComboItem item = (PositionComboItem)cbDetailFilter.getSelectedItem();
                title += item.getValue().getPositionName().toUpperCase();
            } else if (cbFilter.getSelectedItem().equals("Province")) {
                title += "PROVINCE ";
                ProvinceComboItem item = (ProvinceComboItem)cbDetailFilter.getSelectedItem();
                title += item.getValue().getProvinceName().toUpperCase();
            } else if (cbFilter.getSelectedItem().equals("Specialize")) {
                title += "SPECIALIZE ";
                SpecializedComboItem item = (SpecializedComboItem)cbDetailFilter.getSelectedItem();
                title += item.getValue().getSpecializedName().toUpperCase();
            } else if (cbFilter.getSelectedItem().equals("Education level")) {
                title += "EDUCATION LEVEL ";
                EducationComboItem item = (EducationComboItem)cbDetailFilter.getSelectedItem();
                title += item.getValue().getEducationName().toUpperCase();
            }
            new ApplicationCommon().writeToExcell(tbReportEmployee, file,title);
        }
    }//GEN-LAST:event_btnExportEmployeeActionPerformed

    private void cbFilterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbFilterActionPerformed
         DefaultComboBoxModel model = (DefaultComboBoxModel) cbDetailFilter.getModel();
        if (cbFilter.getSelectedIndex() == 0) {
            cbDetailFilter.setEnabled(false);
        } else {
            cbDetailFilter.setEnabled(true);
            if (cbFilter.getSelectedItem().toString().equals("Department")) {
                 int itemCount = cbDetailFilter.getItemCount();
                 cbDetailFilter.removeAllItems();
               
                listDept = new DepartmentList().getAllDepartment();
                for (DepartmentEntity dept : listDept) {
                    model.addElement(new DepartmentComboItem(dept));
                }
                return;
            }
            if (cbFilter.getSelectedItem().toString().equals("Position")) {
                int itemCount = cbDetailFilter.getItemCount();
                cbDetailFilter.removeAllItems();
                listPos = new PositionList().getAllPosition();
                for (PositionEntity pos : listPos) {
                    model.addElement(new PositionComboItem(pos));
                }
                return;
            }
            if (cbFilter.getSelectedItem().toString().equals("Province")) {
                int itemCount = cbDetailFilter.getItemCount();
                cbDetailFilter.removeAllItems();
                listProvince = new ProvinceList().getAllProvince();
                for (ProvinceEntity province : listProvince) {
                    cbDetailFilter.addItem(new ProvinceComboItem(province));
                }
                return;
            }
            if (cbFilter.getSelectedItem().toString().equals("Specialize")) {
                int itemCount = cbDetailFilter.getItemCount();
                cbDetailFilter.removeAllItems();
                listSpecialized = new SpecializedList().getAllSpecialized();
                for (SpecializedEntity specialized : listSpecialized) {
                    cbDetailFilter.addItem(new SpecializedComboItem(specialized));
                }
                return;
            }
            if (cbFilter.getSelectedItem().toString().equals("Education level")) {
                int itemCount = cbDetailFilter.getItemCount();
                cbDetailFilter.removeAllItems();
                listEducation = new EducationList().getAllEducationLevel();
                for (EducationEntity education : listEducation) {
                cbDetailFilter.addItem(new EducationComboItem(education));
                }
                return;
            }

        }

    }//GEN-LAST:event_cbFilterActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnExportEmployee;
    private javax.swing.JButton btnReportEmployee;
    private javax.swing.JComboBox cbDetailFilter;
    private javax.swing.JComboBox cbFilter;
    private com.toedter.calendar.JYearChooser chooserYear;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JPanel pnEmployeeInformation;
    private javax.swing.JPanel pnPayroll;
    private javax.swing.JTabbedPane pnReport;
    private javax.swing.JTable tbReportEmployee;
    private javax.swing.JTable tblReportSalary;
    // End of variables declaration//GEN-END:variables
    DefaultTableModel model; 
    TableRowSorter<TableModel> sorter;
    private TreeMap mapEmployee = new TreeMap<String, EmployeeEntity>();
    private EmployeeEntity selectedEmployee = new EmployeeEntity() ;
    private List<ProvinceEntity> listProvince;
    private List<EducationEntity> listEducation;
    private List<SpecializedEntity> listSpecialized;
    private List<PositionEntity> listPos;
    private List<DepartmentEntity> listDept;
//    private void initModelReport() {
//        sorter = (TableRowSorter<TableModel>) tbReportEmployee.getRowSorter();;
//        this.loadDataTableEmployee();
//        // binding data for department
//        
//    }
//    private int getIndexOfProvince(String name) {
//        int index = 0;
//        for (int i = 0; i< listProvince.size(); i++) {
//            if (listProvince.get(i).getProvinceName().equals(name)) {
//                index = i;
//                break;
//            }
//        }
//        return index;
//    }
//    
//    private int getIndexOfEducation(String name) {
//        int index = 0;
//        for (int i = 0; i< listEducation.size(); i++) {
//            if (listEducation.get(i).getEducationName().equals(name)) {
//                index = i;
//                break;
//            }
//        }
//        return index;
//    }
//    
//    private int getIndexOfSpecialized(String name) {
//        int index = 0;
//        for (int i = 0; i< listSpecialized.size(); i++) {
//            if (listSpecialized.get(i).getSpecializedName().equals(name)) {
//                index = i;
//                break;
//            }
//        }
//        return index;
//    }
//    
//    
//    
//    private int getIndexOfDept(String name) {
//        int index = 0;
//        for (int i = 0; i< listDept.size(); i++) {
//            if (listDept.get(i).getDepartmentName().equals(name)) {
//                index = i;
//                break;
//            }
//        }
//        return index;
//    }
//    
//    private int getIndexOfPos(String name) {
//        int index = 0;
//        for (int i = 0; i< listPos.size(); i++) {
//            if (listPos.get(i).getPositionName().equals(name)) {
//                index = i;
//                break;
//            }
//        }
//        return index;
//    }
    
    private void loadDataTableEmployee() {
        // binding data for employee list      
        model = (DefaultTableModel)tbReportEmployee.getModel();
        model.setRowCount(0);
        List<EmployeeEntity> listEmp = new EmployeeList().getList();
        for (EmployeeEntity item : listEmp) {
            mapEmployee.put(item.getEmployeeId(), item);
            model.addRow(item.toVector());
        }
    }
    
    
    private void reportSalary() {
        
        int year = chooserYear.getYear();
        
        List<ReportSalaryEntity> listReportSalary = new ArrayList<ReportSalaryEntity>();
        
        List<EmployeeEntity> listEmp = new EmployeeList().getList();
        List<Integer> months = new ArrayList<Integer>();
        months.add(Calendar.JANUARY);
        months.add(Calendar.FEBRUARY);
        months.add(Calendar.MARCH);
        months.add(Calendar.APRIL);
        months.add(Calendar.MAY);
        months.add(Calendar.JUNE);
        months.add(Calendar.JULY);
        months.add(Calendar.AUGUST);
        months.add(Calendar.SEPTEMBER);
        months.add(Calendar.OCTOBER);
        months.add(Calendar.NOVEMBER);
        months.add(Calendar.DECEMBER);
        
        for (EmployeeEntity emp : listEmp) {
            ReportSalaryEntity reportSalary = new ReportSalaryEntity();            
            reportSalary.setEmployeeId(emp.getEmployeeId());
            reportSalary.setEmployeeName(emp.getName());
            reportSalary.setDepartment(emp.getDepartmentName());
            reportSalary.setPosition(emp.getPositionName());
            for (Integer month : months) {
                SalaryFormEntity salaryForm = new SalaryFormList().getSalaryFormByDateAndDept(year, month,emp.getDepartmentId());
                if (salaryForm.getSalaryFormId() > 0) {
                  EmployeeSalaryEntity empSalary = new EmployeeSalaryList().getEmployeeSalaryByFormAndEmpId(salaryForm.getSalaryFormId(), emp.getEmployeeId());
                    if (empSalary.getEmployeeSalaryId() > 0) {
                        if (month == Calendar.JANUARY) {
                            reportSalary.setJanuary(empSalary.getTotalSalary());
                        } else if (month == Calendar.FEBRUARY) {
                            reportSalary.setFebruary(empSalary.getTotalSalary());
                        } else if (month == Calendar.MARCH) {
                            reportSalary.setMarch(empSalary.getTotalSalary());
                        } else if (month == Calendar.APRIL) {
                            reportSalary.setApril(empSalary.getTotalSalary());
                        } else if (month == Calendar.MAY) {
                            reportSalary.setMay(empSalary.getTotalSalary());
                        } else if (month == Calendar.JUNE) {
                            reportSalary.setJune(empSalary.getTotalSalary());
                        } else if (month == Calendar.JULY) {
                            reportSalary.setJuly(empSalary.getTotalSalary());
                        } else if (month == Calendar.AUGUST) {
                            reportSalary.setAugust(empSalary.getTotalSalary());
                        } else if (month == Calendar.SEPTEMBER) {
                            reportSalary.setSeptember(empSalary.getTotalSalary());
                        } else if (month == Calendar.OCTOBER) {
                            reportSalary.setOctober(empSalary.getTotalSalary());
                        } else if (month == Calendar.NOVEMBER) {
                            reportSalary.setNovember(empSalary.getTotalSalary());
                        } else if (month == Calendar.DECEMBER) {
                            reportSalary.setDecember(empSalary.getTotalSalary());
                        }
                    }
                }
            }
            reportSalary.setTotal(reportSalary.getJanuary().add(reportSalary.getFebruary()).add(reportSalary.getMarch()).add(reportSalary.getApril()).add(reportSalary.getMay()).add(reportSalary.getJune()).add(reportSalary.getJuly()).add(reportSalary.getAugust()).add(reportSalary.getSeptember()).add(reportSalary.getOctober()).add(reportSalary.getNovember()).add(reportSalary.getDecember()));               
            listReportSalary.add(reportSalary);
        }
        DefaultTableModel model = (DefaultTableModel)tblReportSalary.getModel();
        model.setRowCount(0);
        
        for (ReportSalaryEntity report : listReportSalary) {
            model.addRow(report.toVector());
        }
        
    }
    
}

